class Email < ActiveRecord::Base
  belongs_to  :user
  
  def before_create
    self.verify_code=Digest::SHA1.hexdigest(self.email+"EmailVerify"+rand.to_s)
    self.verify_sent=Time.now
    AccountMailer.deliver_verify(self.user.fullname, self.email,self.verify_code,self.verify_sent)
  end
  
  def generate_hash
    self.verify_code=Digest::SHA1.hexdigest(self.email+"EmailVerify"+rand.to_s)
    AccountMailer.deliver_verify(self.email,self.verify_code,self.verify_code,self.verify_sent)
    self.verify_sent=Time.now   
  end
  
  def self.validate(email)
    #in email_format and is unique
    if !(email=~/^[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+[\.a-zA-Z]+$/)
      status=0
    elsif !(Email.find_by_email(email).nil?)
      status=1
    else
      status=2
    end
    return status
  end
end